;
(function($) {
	var dataMap = {};
	var getNextId = (function() {
		var id = 0;
		return function() {
			return ++id;
		};
	})();
	function deleteObj(obj) {
		if (obj == null) {
			return;
		}
		var jSelect = $(obj.target);
		jSelect.remove();
		var id = jSelect.attr("multiSelectId");
		delete dataMap[id];
	}
	function createMultiSelect1(jSelect, data, defValue) {
		var obj, id;
		if (!jSelect) {
			jSelect = $("<select></select>");
			id = getNextId();
			jSelect.attr("multiSelectId", id);
			obj = {
				id : id,
				target : jSelect.get()[0]
			};
			dataMap[id] = obj;
		} else {
			id = jSelect.attr("multiSelectId");
			if (!id) {
				id = getNextId();
				jSelect.attr("multiSelectId", id);
				obj = {
					id : id,
					target : jSelect.get()[0]
				};
				dataMap[id] = obj;
			} else {
				obj = dataMap[id];
			}
		}
		obj.data = data;
		jSelect.empty();
		jSelect.append("<option value='-1'>--请选择--</option>");
		for ( var i = 0; i < data.length; i++) {
			var value = data[i].id;
			var text = data[i].text;
			jSelect.append("<option value='" + value + "'>" + text
					+ "</option>");
		}
		jSelect.val(defValue);

		// //变动时的处理
		jSelect
				.change(function() {
					var thisValue = $(this).val();
					var thisId = $(this).attr("multiSelectId");
					var thisObj = dataMap[thisId];
					var thisSelect = $(thisObj.target);// thisSelect -->
														// $(this)
					var thisData = thisObj.data;
					var nextObj = null;
					while (thisObj.next) {
						nextObj = thisObj.next;
						if (nextObj.next) {
							thisObj.next = nextObj.next;
							$(nextObj.target).remove();
							delete dataMap[nextObj.id];
						} else {
							nextObj.prev = thisObj;
							break;
						}
					}
					if (thisValue != -1) {
						var selectedData = null;
						for ( var i = 0; i < thisData.length; i++) {
							if (thisData[i].id == thisValue) {
								selectedData = thisData[i];
								break;
							}
						}
						if (!!selectedData.subs && selectedData.subs.length > 0) {
							if (nextObj) {
								createMultiSelect1($(nextObj.target),
										selectedData.subs, -1);
							} else {// 这就是最后一个，则要提升一级
								var obj = createMultiSelect1(null, thisData,
										thisValue);
								obj.prev = thisObj.prev;
								obj.next = thisObj;
								if (thisObj.prev) {
									thisObj.prev.next = obj;
								}
								thisObj.prev = obj;
								createMultiSelect1($(thisObj.target),
										selectedData.subs, -1);
								$(obj.target).insertBefore(thisSelect);
							}
						} else {// 删除当前select
							if (nextObj) {
								createMultiSelect1($(nextObj.target), thisData,
										thisValue);
								nextObj.prev = thisObj.prev;
								if (thisObj.prev) {
									thisObj.prev.next = nextObj;
								}
								deleteObj(thisObj);
							}
						}
					} else if (nextObj) {
						createMultiSelect1($(nextObj.target), thisData,
								thisValue);
						nextObj.prev = thisObj.prev;
						if (thisObj.prev) {
							thisObj.prev.next = nextObj;
						}
						deleteObj(thisObj);
					}
				});
		return obj;
	}
	function initMultiSelect(select, data, defValue) {
		var index = -1;
		for ( var i = 0; i < data.length; i++) {
			if (data[i].id == defValue) {
				index = i;
				break;
			}
		}
		if (index >= 0) {
			var obj = createMultiSelect1(select, data, defValue);
			return obj;
		} else
			for ( var i = 0; i < data.length; i++) {
				var subs = data[i].subs;
				if (!!subs && subs.length > 0) {
					var obj = initMultiSelect(select, subs, defValue);
					if (obj != null) {
						var thisObj = initMultiSelect(null, data, data[i].id);
						thisObj.next = obj;
						obj.prev = thisObj;
						$(thisObj.target).insertBefore($(obj.target));
						return thisObj;
					}
				}
			}
		return null;
	}

	$.fn.extend({
		"createSelect" : function(url, opt, func, dv, callback) {
			var self = $(this);
			if (arguments.length == 2) {
				var arr = url;
				var defaultValue = opt;
				self.each(function(i, select) {
					for ( var i = 0; i < arr.length; i++) {
						var value = arr[i][0];
						var text = arr[i][1];
						$(select).append(
								"<option value='" + value + "'>" + text
										+ "</option>");
					}
					if (defaultValue != null) {
						$(select).val(defaultValue);
					}
				});
			} else {
				// 通过ajax向服务器端申请数据
				var option = $.extend({
					pageNo : 1,
					pageSize : 999,
					sortName : "id",
					sortOrder : "asc",
					query : null
				}, opt || {});
				$.post(url, option, function(result) {
					if (!!result.total) {
						for ( var i = 0; i < result.rows.length; i++) {
							var row = result.rows[i];
							var layout = func(row);
							self.each(function(index, select) {
								$(select).append(
										"<option value='" + layout[0] + "'>"
												+ layout[1] + "</option>");
							});
						}
					}
					if (dv != null) {
						self.val(dv);
					}
					if (!!callback) {
						callback();
					}
				});
			}
			return self;
		},
		"createMultiSelect" : function(data, defValue) {
			if (defValue != -1) {
				initMultiSelect($(this), data, defValue);
			} else {
				createMultiSelect1($(this), data, -1);
			}
		}
	});
})(jQuery);